﻿Imports DARSClassLibrary

Public Class What_If_Major
    Inherits System.Web.UI.Page
    Dim ws As New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice
    Dim progressArray As New ArrayList

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if user is logged in
        If Session("CurrentUser") Is Nothing Then
            Response.Redirect("NotLoggedIn.aspx")
            Exit Sub
        End If

        'Loads the Student object from session and displays his/her name
        Dim objStudent As Student
        objStudent = CType(Session("CurrentUser"), Student)
        lblUser.Text = objStudent.LastName & ", " & objStudent.FirstName

        'Retrieves major information for the student
        Dim dsStdMajor As DataSet
        dsStdMajor = ws.getAllMajors()
        Session("dsStdMajor") = dsStdMajor

        'Retrieves the student's course record
        Dim dsStudCrsRec As DataSet
        dsStudCrsRec = ws.getStudentCourseRecord(objStudent.studentID)
        Session("dsStudCrsRec") = dsStudCrsRec

        'Retrieves all available majors
        Dim dsAllMajors As DataSet
        dsAllMajors = ws.getAllMajors()
        Session("dsAllMajors") = dsAllMajors

        If Not IsPostBack Then
            'Binds all majors to the dropdownlist
            ddlMajors.DataSource = dsStdMajor
            ddlMajors.DataTextField = "MajDegID"
            ddlMajors.DataValueField = "MajDegID"
            ddlMajors.DataBind()

            'Stores the current major in Session if this is the first pageload
            Session("MajorID") = dsStdMajor.Tables(0).Rows(0)("MajorID")
            Session("DegreeID") = dsStdMajor.Tables(0).Rows(0)("DegreeID")
        End If

    End Sub

    Private Sub loadProgress()

        Dim dsStudCrsRec As DataSet = CType(Session("dsStudCrsRec"), DataSet)

        If progressArray.Count > 0 Then
            progressArray.Clear()
        End If

        'Displays the current major
        lblCurrentMajor.Text = CStr(Session("MajorID")) & " (" & CStr(Session("DegreeID")) & ")"

        'Retrieves the path requirements for the student's selected major
        Dim dsPathReq As DataSet
        dsPathReq = ws.getPathRequirements(CStr(Session("MajorID")), CStr(Session("DegreeID")), CInt(Session("PathID")))
        dsPathReq.Tables(0).TableName = "PathReqs"

        'Creates an additional column for PathReqs for the number of courses required for
        ' each MD Requirement
        Dim dc0 As New DataColumn("NumberCoursesRequired")
        dsPathReq.Tables("PathReqs").Columns.Add(dc0)

        'Populates the NumberCoursesRequired column in PathReqs
        For i As Integer = 0 To dsPathReq.Tables("PathReqs").Rows.Count - 1
            Dim count As Integer
            count = ws.getMajorDegreeReqNumberCoursesRequired(CStr(dsPathReq.Tables("PathReqs").Rows(i)("MajorDegreeRequirement_MajorID")), CStr(dsPathReq.Tables("PathReqs").Rows(i)("MajorDegreeRequirement_DegreeID")), CInt(dsPathReq.Tables("PathReqs").Rows(i)("MajorDegreeRequirement_RequirementID")))
            dsPathReq.Tables("PathReqs").Rows(i)("NumberCoursesRequired") = count
        Next
        Session("dsPathReq") = dsPathReq

        'Creates a DataTable for CourseRequirements
        Dim dtReqCourses As New DataTable("CourseReqs")
        Dim dc1 As New DataColumn("MajDegReqID")
        Dim dc2 As New DataColumn("CourseID")
        dtReqCourses.Columns.Add(dc1)
        dtReqCourses.Columns.Add(dc2)

        'Populates the Course Requirement DataTable
        For i As Integer = 0 To dsPathReq.Tables("PathReqs").Rows.Count - 1
            Dim dsReqCourseList As DataSet
            dsReqCourseList = ws.getRequiredCourses(CStr(Session("MajorID")), _
                                                    CStr(Session("DegreeID")), _
                                                    CStr(dsPathReq.Tables("PathReqs").Rows(i)("MajorDegreeRequirement_RequirementID")))
            If dsReqCourseList.Tables(0).Rows.Count > 0 Then
                For j As Integer = 0 To dsReqCourseList.Tables(0).Rows.Count - 1
                    Dim dataRow As DataRow = dtReqCourses.NewRow
                    dataRow("MajDegReqID") = dsReqCourseList.Tables(0).Rows(j)("MajDegReqID")
                    dataRow("CourseID") = dsReqCourseList.Tables(0).Rows(j)("CourseID")
                    dtReqCourses.Rows.Add(dataRow)
                Next
            End If
        Next
        Dim dsCompareReqs As New DataSet
        dsCompareReqs.Tables.Add(dtReqCourses)

        'Compares the student's course history with the list of course requirements
        Dim objCompare As New Compare
        Dim dsCompareResult As DataSet
        dsCompareResult = objCompare.Compare(dsStudCrsRec, dsCompareReqs, dsPathReq)
        dsPathReq.Tables.Add(dsCompareResult.Tables("Results").Copy)
        Session("objCompare") = objCompare

        'Creates a relationship between the Path Requirements and Comparison Results tables
        dsPathReq.Relations.Add("relation1", dsPathReq.Tables("PathReqs").Columns("MajDegReqID"), dsPathReq.Tables("Results").Columns("MajDegReqID"))
        Session("dsPathReq") = dsPathReq

        'Binds the PathRequirements dataset to the progress repeater
        rptPathReqs.DataSource = dsPathReq
        rptPathReqs.DataBind()

        'Retrieves and displays the overall major completion
        Dim percentComplete As Decimal
        Dim completionSum As Decimal
        For i As Integer = 0 To progressArray.Count - 1
            completionSum = completionSum + CDec(progressArray.Item(i))
        Next
        percentComplete = completionSum / progressArray.Count
        litTotalPercentComplete.Text = "Major " & FormatNumber(percentComplete, 1) & "% Complete"

    End Sub
    Private Sub rptPathReqs_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPathReqs.ItemDataBound

        Dim dsPathReq As DataSet = CType(Session("dsPathReq"), DataSet)
        Dim dsStdMajor As DataSet = CType(Session("dsStdMajor"), DataSet)

        Dim item As RepeaterItem = e.Item

        'Displays course requirements and progress for each Major-Degree Requirement
        If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
            Dim rptResults As Repeater = CType(item.FindControl("rptResults"), Repeater)
            Dim dv As DataRowView = CType(e.Item.DataItem, DataRowView)
            rptResults.DataSource = dv.CreateChildView("relation1")
            rptResults.DataBind()

            Dim objCompare As Compare = CType(Session("objCompare"), Compare)
            Dim mdrID As String = CStr(dv.Row("MajDegReqID"))
            Dim percentComplete As Decimal = objCompare.getPercentComplete(mdrID, CType(Session("dsPathReq"), DataSet))
            Dim literalCtrl As Literal = CType(item.FindControl("litPercentComplete"), Literal)
            literalCtrl.Text = FormatNumber(percentComplete, 1) & "% Complete"

            'Stores the percent complete in an arraylist
            progressArray.Add(percentComplete)
        End If

    End Sub

    Protected Sub ddlChooseMajor_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlMajors.SelectedIndexChanged

        btnSubmit.Visible = False

        Dim dsStdMajor As DataSet = CType(Session("dsStdMajor"), DataSet)
        Dim majDegID As String = ddlMajors.SelectedValue()

        'Stores the selected major-degree's information in Session
        For i As Integer = 0 To dsStdMajor.Tables(0).Rows.Count - 1
            If String.Equals(dsStdMajor.Tables(0).Rows(i)("MajDegID"), majDegID) Then
                Session("MajorID") = dsStdMajor.Tables(0).Rows(i)("MajorID")
                Session("DegreeID") = dsStdMajor.Tables(0).Rows(i)("DegreeID")
                Exit For
            End If
        Next

        ddlPaths.Items.Clear()
        ddlPaths.Items.Add("Select a Path:")

        Dim dsMajPaths As DataSet = ws.getMajorDegreePaths(CStr(Session("MajorID")), CStr(Session("DegreeID")))
        ddlPaths.DataSource = dsMajPaths
        Session("dsMajPaths") = dsMajPaths
        ddlPaths.DataTextField = "PathID"
        ddlPaths.DataValueField = "PathID"
        ddlPaths.DataBind()
        ddlPaths.Visible = True

    End Sub

    Protected Sub ddlChoosePath_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPaths.SelectedIndexChanged

        Dim dsMajPaths As DataSet = CType(Session("dsMajPaths"), DataSet)
        Dim PathID As String = ddlPaths.SelectedValue()

        'Stores the selected path's information in Session
        Session("PathID") = CInt(PathID)

        btnSubmit.Visible = True

    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        loadProgress()

    End Sub

    Protected Sub logout(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogout.ServerClick
        Session.Abandon()
        Response.Redirect("NotLoggedIn.aspx")
    End Sub

    Protected Sub home(ByVal sender As Object, ByVal e As EventArgs) Handles btnhome.ServerClick
        Response.Redirect("LandingPage.aspx")
    End Sub

End Class